﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerFixedAssetCategoryDao : IFixedAssetCategoryDao
    {
        /// <summary>
        /// Gets the accounts for is inventory item.
        /// </summary>
        /// <returns>List{FixedAssetCategory}.</returns>
        public List<FixedAssetCategory> GetFixedAssetCategorysForIsInventoryItem()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        public List<FixedAssetCategory> GetFixedAssetCategorysByUnit(long unitId)
        {
            const string sql = @"uspGet_FixedAssetCategory_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
        public List<FixedAssetCategory> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_FixedAssetCategory";
            object[] parms = { "@WhereClause", null, "@OrderBy", "FixedAssetCategoryCode", "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_FixedAssetCategory";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        string IFixedAssetCategoryDao.InsertFixedAssetCategory(FixedAssetCategory fixedAsset)
        {
            const string sql = "uspInsert_FixedAssetCategory";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(fixedAsset));
        }


        public string UpdateFixedAssetCategory(FixedAssetCategory fixedAsset)
        {
            const string sql = "uspUpdate_FixedAssetCategory";
            return Db.Update(sql, CommandType.StoredProcedure, Take(fixedAsset));
        }

        public string DeleteFixedAssetCategory(FixedAssetCategory fixedAsset)
        {
            const string sql = @"uspDelete_FixedAssetCategory";
            object[] parms = { "@FixedAssetCategoryID", fixedAsset.FixedAssetCategoryId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }


        public FixedAssetCategory GetFixedAssetCategory(long fixedAssetId)
        {
            const string sql = @"uspGet_FixedAssetCategory_ByID";
            object[] parms = { "@FixedAssetCategoryID", fixedAssetId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<FixedAssetCategory> GetFixedAssetCategorysByParent(long parentId)
        {
            const string sql = @"uspGet_FixedAssetCategory_ByCondition";
            object[] parms = { "@WhereClause", " WHERE ParentID = " + parentId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<FixedAssetCategory> GetFixedAssetCategorysForComboTree(int fixedAssetCategoryId)
        {
            //const string sql = @"uspGet_FixedAssetCategory_ForComboTreee";
            //object[] parms = { "@FixedAssetCategoryID", fixedAssetCategoryId };
            //return Db.ReadList(sql, true, Make, parms);

            const string sql = @"uspGet_FixedAssetCategory_ByUnit";
            object[] parms = { "@UnitID", 1 };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, FixedAssetCategory> Make = reader =>
            new FixedAssetCategory
            {
                FixedAssetCategoryId = reader["FixedAssetCategoryID"].AsInt(),
                ParentId = reader["ParentID"].AsInt(),
                FixedAssetCategoryCode = reader["FixedAssetCategoryCode"].AsString(),
                FixedAssetCategoryName = reader["FixedAssetCategoryName"].AsString(),
                DepreciationRate = reader["DepreciationRate"].AsDecimal(),
                LifeTime = reader["LifeTime"].AsDecimal(),
                Grade = reader["Grade"].AsInt(),
                IsParent = reader["IsParent"].AsBool(),
                OrgPriceAccountCode = reader["OrgPriceAccountCode"].AsString(),
                DepreciationAccountCode = reader["DepreciationAccountCode"].AsString(),
                CapitalAccountCode = reader["CapitalAccountCode"].AsString(),
                IsActive = reader["IsActive"].AsBool(),
                UnitId = reader["UnitID"].AsInt()
            };

        /// <summary>
        /// Takes the specified account.
        /// </summary>
        /// <returns></returns>
        private object[] Take(FixedAssetCategory fixedAssetCategory)
        {
            return new object[]  
            {
                "@FixedAssetCategoryID", fixedAssetCategory.FixedAssetCategoryId,
                "@ParentID", fixedAssetCategory.ParentId,
                "@FixedAssetCategoryCode", fixedAssetCategory.FixedAssetCategoryCode,
                "@FixedAssetCategoryName", fixedAssetCategory.FixedAssetCategoryName,
                "@DepreciationRate", fixedAssetCategory.DepreciationRate,
                "@LifeTime", fixedAssetCategory.LifeTime,
                "@Grade", fixedAssetCategory.Grade,
                "@IsParent", fixedAssetCategory.IsParent,
                "@OrgPriceAccountCode", fixedAssetCategory.OrgPriceAccountCode,
                "@DepreciationAccountCode", fixedAssetCategory.DepreciationAccountCode,
                "@CapitalAccountCode", fixedAssetCategory.CapitalAccountCode,
                "@UnitID", fixedAssetCategory.UnitId,
                "@IsActive", fixedAssetCategory.IsActive
            };
        }
    }
}
